package StudyCode.summary.mydatetime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class DateTimeFormatterParseBest {
	public static void main(String[] args) {
		DateTimeFormatter parser = DateTimeFormatter.ofPattern("yyyy-MM-dd['T'HH:mm:ss[Z]]");
		parseStr(parser, "2019-05-31");
		parseStr(parser, "2019-05-31T16:30:12");
		parseStr(parser, "2019-05-31T16:30:12-0500");
	}

	private static void parseStr(DateTimeFormatter formatter, String text) {
		try {
			TemporalAccessor ta = formatter.parseBest(text, OffsetDateTime::from, LocalDateTime::from, LocalDate::from);
			if (ta instanceof OffsetDateTime) {
				OffsetDateTime odt = OffsetDateTime.from(ta);
				System.out.println("OffsetDateTime: " + odt);
			} else if (ta instanceof LocalDateTime) {
				LocalDateTime ldt = LocalDateTime.from(ta);
				System.out.println("LocalDateTime: " + ldt);
			} else if (ta instanceof LocalDate) {
				LocalDate ld = LocalDate.from(ta);
				System.out.println("LocalDate: " + ld);
			}
		} catch (DateTimeParseException e) {
			e.printStackTrace();
		}
	}
}
